<?php

//$Timer->start($GlobPart);

if($_POST['act'] == 'edititem')
{	$ItemCurrent = getRowByField($WorkTable, $WorkTableKeyFieldName, $_REQUEST[$WorkTableKeyVarName]);
	$MySQLFuncColumns = array();
  $BreakColumns = array();
  foreach($_SQL_TABLE_FIELDS[$GlobPart] as $field => $farr)
  {  	$farr['currentValue'] = $ItemCurrent[$field];
    
  	if($farr['type'] == 'image')
    {
    	if($farr['useFTP'] == 'y')
    	{
    		require_once __CFG_PATH_LIBS.__CFG_PATH_CORE_CLASSES."class.FTPImage.php";
    		$ftpi = new FTPImage($farr['ftpServer'], $farr['ftpUserName'], $farr['ftpPassword'], $farr['ftpRootPath']);
    		
    		$farr['addVariable'] = $ftpi->editItemFieldHandler($field, $farr, $_FILES);
    		if($ftpi->hasErrors())
    		{
    			$ErrorFlag = __TRUE;
    			$ErrorMessages = array_merge($ErrorMessages, $ftpi->getErrorMessages());
    		}
    		if(!$farr['addVariable'])
    		{
    			$BreakColumns[] = $field;
    		}
    	}
    }
    
    if(!isBlank($farr['required']) && !preg_match("/^".$farr['required']."$/Uis", $farr['addVariable']) && !preg_match("/^".$farr['required']."$/Uis", $farr['editVariable']) && !strstr($farr['editFormOther'],'disabled'))
    {
      $ErrorFlag = __TRUE;
      $ErrorMessage = $ERRORS['admin'][__ERROR_INCORRECT_REQUIRED_FIELDS];
      $repl_arr = array('field' => $farr['title']);
      replaceVariables($repl_arr, $ErrorMessage);
      $ErrorMessages[] = $ErrorMessage;
    }
    elseif(!isBlank($farr['notEmptyRequired']) && !preg_match("/^".$farr['notEmptyRequired']."$/Uis", $farr['addVariable']) && !isBlank($farr['addVariable'])  || !preg_match("/^".$farr['notEmptyRequired']."$/Uis", $farr['editVariable']) && !strstr($farr['editFormOther'],'disabled') && !isBlank($farr['editVariable']))
    {
      $ErrorFlag = __TRUE;
      $ErrorMessage = $ERRORS['admin'][__ERROR_INCORRECT_REQUIRED_FIELDS];
      $repl_arr = array('field' => $farr['title']);
      replaceVariables($repl_arr, $ErrorMessage);
      $ErrorMessages[] = $ErrorMessage;
    }
    if(isBlank($farr['noUseInEdit']) && !isBlank($farr['editVariable']) && isBlank($farr['listOfRelations']) && isBlank($farr['isRelation']))
    {      $result[$field] = $farr['editVariable'];
      if($farr['editVarType'] == $CONFIG['VarTypeSQLFunction'])
      {      	$MySQLFuncColumns[] = $field;
      }
    }
    elseif(isBlank($farr['noUseInEdit']) && !isset($farr['editVariable']) && !isBlank($farr['addVariable']) && isBlank($farr['listOfRelations']) && isBlank($farr['isRelation']))
    {      $result[$field] = $farr['addVariable'];
      if($farr['addVarType'] == $CONFIG['VarTypeSQLFunction'])
      {
      	$MySQLFuncColumns[] = $field;
      }
    }
    elseif(isBlank($farr['noUseInEdit']) && isBlank($farr['editVariable']) && isBlank($farr['addVariable']) /*&& $farr['allowEmpty'] == 'y'*/ && isBlank($farr['listOfRelations']) && isBlank($farr['isRelation']) && !strstr($farr['editFormOther'],'disabled'))
    {      $result[$field] = $farr['addVariable'];
    }
    elseif($farr['listOfRelations'] == 'y' && isBlank($farr['noUseInEdit']) && isBlank($farr['isRelation']))
    {      makeListOfRelations($farr['tableInfo'], ((!isBlank($farr['editVariable'])) ? $farr['editVariable'] : $farr['addVariable']));
      $BreakColumns[] = $field;
    }
    elseif($farr['isRelation'] == 'y' && isBlank($farr['noUseInEdit']))
    {
      makeRelation($farr['tableInfo'], ((!isBlank($farr['editVariable'])) ? $farr['editVariable'] : $farr['addVariable']));
      $BreakColumns[] = $field;
    }

    if($farr['notUsedInDB'] == 'y')
    {      $BreakColumns[] = $field;
    }
    if($farr['md5'] == 'y' && strlen($result[$field]) != 32)
    {
      $result[$field] = md5($result[$field]);
    }

    if($farr['unique'] == 'y' && !strstr($farr['editFormOther'],'disabled'))
    {
      $UniqueResult = getRowByField($WorkTable, $field, $result[$field]);
      if(stripslashes(trim($UniqueResult[$field])) == stripslashes(trim($result[$field])) && $UniqueResult[$WorkTableKeyFieldName] != $_REQUEST[$WorkTableKeyVarName])
      {
        $ErrorFlag = __TRUE;
        $repl_arr = array('field' => $farr['title']);
        $ErrorMessage = $ERRORS['admin'][__ERROR_DUPLICATED_FIELD_VALUE];
        replaceVariables($repl_arr, $ErrorMessage);
        $ErrorMessages[] = $ErrorMessage;
      }
    }
  }
  if($ErrorFlag != __TRUE)
  {
    if(is_callable($CONFIG['AdminAddFuncEditItem']))
    {
      $OldRow = getRowByField($WorkTable, $WorkTableKeyFieldName, $_REQUEST[$WorkTableKeyVarName]);
    }
    makeUpdateList($strSet, $result, $MySQLFuncColumns, $BreakColumns);
    updateItem($WorkTable, $strSet, $WorkTableKeyFieldName, $_REQUEST[$WorkTableKeyVarName]);
    if(is_callable($CONFIG['AdminAddFuncEditItem']))
    {
      $NewRow = getRowByField($WorkTable, $WorkTableKeyFieldName, $_REQUEST[$WorkTableKeyVarName]);
      eval($CONFIG['AdminAddFuncEditItem'].'();');
    }
    if($_FLAGS['JVSCMessage'] == __TRUE)
    {      go_toJVSC(getSameUri());
    }
    else
    {
      go_to(getSameUri());
    }
  	exit();
  }
  /*else
  {  	//$tpl->assign('ErrorMessage', $ErrorMessage);
  }*/
}
elseif($_POST['act'] == 'status')
{
  $result = array(
  	       			$_POST[varname] => $_POST[varvalue],
  				);
  if(!isEmptyArr($_SQL_TABLE_FIELDS[$GlobPart]['date']))
  {
  	$result['date'] = 'NOW()';
  }
  makeUpdateList($strSet, $result, array('date'));
  updateItem($WorkTable, $strSet, $WorkTableKeyFieldName, $_POST[$WorkTableKeyVarName]);
  go_to(getSameUri());
  exit();
}
elseif($_POST['act'] == 'editfield')
{
  $result = array(
  	   $_POST['varname'] => ($_POST['varname'] == 'linkname') ? make_linkname($_POST['varvalue']) : $_POST['varvalue'],
  );
  if(!isEmptyArr($_SQL_TABLE_FIELDS[$GlobPart]['date']))
  {
  	$result['date'] = 'NOW()';
  }
  makeUpdateList($strSet, $result, array('date'));
  updateItem($WorkTable, $strSet, $WorkTableKeyFieldName, $_POST[$WorkTableKeyVarName]);
  go_to(getSameUri());
  exit();
}
elseif($_POST['act'] == 'deleteimage')
{	$ItemCurrent = getRowByField($WorkTable, $WorkTableKeyFieldName, $_REQUEST[$WorkTableKeyVarName]);
	$farr = $_SQL_TABLE_FIELDS[$GlobPart][$_POST['fieldName']];
	if($farr['useFTP'] == 'y')
    {
    	require_once __CFG_PATH_LIBS.__CFG_PATH_CORE_CLASSES."class.FTPImage.php";
    	$ftpi = new FTPImage($farr['ftpServer'], $farr['ftpUserName'], $farr['ftpPassword'], $farr['ftpRootPath']); 
    	$deleted = $ftpi->deleteFile($ItemCurrent[$_POST['fieldName']]);
    }
    $result = array(
   		$_POST['fieldName'] => '',
    );
    makeUpdateList($strSet, $result, array('date'));
    updateItem($WorkTable, $strSet, $WorkTableKeyFieldName, $_REQUEST[$WorkTableKeyVarName]);
	
	go_to(getSameUri());
	exit();
}
elseif(is_callable($CONFIG['ItemActionFuncPrefix'].$_POST['act']))
{
  eval($CONFIG['ItemActionFuncPrefix'].$_POST['act'].'();');
}

if(!isBlank($_GET[$CONFIG['AdminActionGetVar']]) && is_callable($CONFIG['ItemActionFuncPrefix'].$_GET[$CONFIG['AdminActionGetVar']]))
{
  eval($CONFIG['ItemActionFuncPrefix'].$_GET[$CONFIG['AdminActionGetVar']].'();');
}

if($_FLAGS['NoReadDB'] == __FALSE)
{
  $Item = getRowByField($WorkTable, $WorkTableKeyFieldName, $_REQUEST[$WorkTableKeyVarName]);
}
if(is_callable($CONFIG['AdminAddFuncGetItem']))
{   eval($CONFIG['AdminAddFuncGetItem'].'();');
}

if($ErrorFlag == __TRUE)
{  foreach($Item as $key => $val)
  {
    if(isset($_POST[$key]))
    {
      $Item[$key] = $_POST[$key];
    }
  }
}
$tpl->assign_by_ref("Item", $Item);

eval('$PageTitle = '.$PageTitle);
$tpl->assign("PageTitle", $PageTitle);

$tpl->assign_by_ref("ConfFields", $_SQL_TABLE_FIELDS[$GlobPart]);
$tpl->assign_by_ref("ConfEditForms", $_SQL_TABLE_EDIT_FORMS[$GlobPart]);
$tpl->assign_by_ref("GlobPart", $GlobPart);

if($_FLAGS['NotChangeItemTemplate'] != __TRUE)
{
  $BodyTemplate = 'admin.body_item.tpl';
}

$tpl->assign("BodyTemplate", $BodyTemplate);

//$Timer->stop($GlobPart);
//$Timer->display();
if($tpl->template_exists("admin.".$GlobPart.".tpl"))
{
  $_ADMIN_SMARTY_TEMPLATE = "admin.".$GlobPart.".tpl";
}
else
{  $_ADMIN_SMARTY_TEMPLATE = "admin.item.tpl";
}

?>